package com.cs.beneficios.web.action;

import com.cs.paoyer.administration.business.model.UserVO;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.struts.action.Action;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import com.cs.beneficios.common.ConnectionFactory;

public class ModSedeAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String valor1 = null, valor2 = null, valor3 = null, valor4 = null, valor5 = null;
		String valor6 = null, valor7 = null, valor8 = null, valor9 = null, valor10 = null, valor11 = null;
		ConnectionFactory cf = new ConnectionFactory();
		HttpSession session = request.getSession();
		Connection conn = null;

		try {
			conn = cf.getConnection();
		} catch (SQLException e) {
		}

		String strsede = request.getParameter("codsede");
		try {
			valor1 = request.getParameter("sede");
		} catch (Exception e) {
			valor1 = "";
		}
		try {
			valor2 = request.getParameter("descripcion");
		} catch (Exception e) {
			valor2 = "";
		}
		try {
			valor3 = request.getParameter("code");
		} catch (Exception e) {
			valor3 = "";
		}
		try {
			valor4 = request.getParameter("tipo");
		} catch (Exception e) {
			valor4 = "";
		}
		try {
			valor5 = request.getParameter("caracter");
		} catch (Exception e) {
			valor5 = "";
		}
		try {
			valor11 = request.getParameter("dirdesc");
		} catch (Exception e) {
			valor11 = "";

		}
		try {
			valor6 = request.getParameter("dirEncasillada");
		} catch (Exception e) {
			valor6 = "";
		}
		try {
			valor10 = request.getParameter("cobama");
		} catch (Exception e) {
			valor10 = "";
		}
		try {
			valor7 = request.getParameter("desde");
		} catch (Exception e) {
			valor7 = "";
		}
		try {
			valor8 = request.getParameter("hasta");
		} catch (Exception e) {
			valor8 = "";
		}
		try {
			valor9 = request.getParameter("vigencia");
		} catch (Exception e) {
			valor9 = "";
		}

		String strSql = " UPDATE TCS_BEN_SEDE SET nombre = '"
				+ valor1
				+ "', caracter = '"
				+ valor5
				+ "', "
				+ " direccion_descriptiva = '"
				+ valor11
				+ "', desde_vigencia = TO_DATE('"
				+ valor7
				+ "','dd/mm/yyyy'), "
				+ " VFL_CODIGO_MUNICIPIO = "
				+ valor3
				+ ",hasta_vigencia = TO_DATE('"
				+ valor8
				+ "','dd/mm/yyyy'), vigencia = '"
				+ valor9
				+ "', "
				+ " descripcion = '"
				+ valor2
				+ "', tipo = '"
				+ valor4
				+ "',direccion_encasillada = '"
				+ valor6
				+ "', localizacion_cobama = '"
				+ valor10
				+ "', "
				+ " USUARIO_MODIFICA = ?, FECHA_MODIFICA = to_date(?,'dd/mm/yyyy'), "
				+ " TERMINAL_MODIFICA = ? " + " WHERE codigo = ? ";
		// Proceso para la auditoria
		UserVO user = (UserVO) request.getSession().getAttribute("userInfo");
		String terminalAddress = request.getRemoteAddr();
		String strTerminal = ("WEB=" + terminalAddress);
		Calendar c = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		// Fin del proceso
		String strPar[] = { user.getName(), dateFormat.format(c.getTime()),
				strTerminal, strsede };

		try {
			int modificar = cf.execSql(strSql, strPar, conn);
			if (modificar > 0) {
				session.setAttribute("mensajes",
						"La Sede fue modificada correctamente!!!");
				session.setAttribute("primeraVez", null);
			}
			cf.closeConnection(conn);
		} catch (SQLException e) {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSUB - cwsub ********/");
			session.setAttribute("mensajes", "La Sede no se pudo modificar.");
		}

		return mapping.findForward("success");
	}
}